package Lee_code;

public class Lee_200 {

    public static int numIslands(char[][] grid) {
        int row = grid.length;
        int column = grid[0].length;
        int count=0;

        for(int i=0; i<row; i++)
        {
            for(int j=0; j<column; j++)
            {
                if(grid[i][j]=='1')
                {
                    count++;
                    dfs(grid, i, j);
                }
            }
        }
        return  count;
    }

    public static void dfs(char[][] grid, int i, int j)
    {
        int row = grid.length;
        int column = grid[0].length;

        if(i<0||i>row||j<0||j>column)
        {
            return;
        }
        else if(grid[i][j]=='0')
        {
            return;
        }
        else
        {
            grid[i][j]='0';
            dfs(grid, i+1, j);
            dfs(grid, i-1, j);
            dfs(grid, i, j+1);
            dfs(grid, i, j-1);
        }
    }


    public static void main(String[] args) {
        char[][] grid = { {'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};
        System.out.println(numIslands(grid));


    }
}
